#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

// #define FIFO_FIFO "fifo"

#define PATH "."
#define FIFONAME "fifo"

class IpcFifo
{
public:
    IpcFifo(const std::string &path, const std::string &name)
        : _path(path), _name(name)
    {

        _fifoname = _path + "/" + _name;
        // 权限掩码
        umask(0);
        // 创建命名管道
        int n = mkfifo(_fifoname.c_str(), 0666);
        if (n < 0)
        {
            std::cout << "mkfifo error" << std::endl;
        }
        else
        {
            std::cout << "mkfifo successful" << std::endl;
        }
    }
    ~IpcFifo()
    {
        // 删除管道文件
        int n = unlink(_fifoname.c_str());
        if (n == 0)
        {
            std::cout << "remove mkfifo successful" << std::endl;
        }
        else
        {
            std::cout << "remove mkfifo failed" << std::endl;
        }
    }

private:
    std::string _name;
    std::string _path;
    std::string _fifoname;
};

class FifoOper
{
public:
    FifoOper(const std::string &path, const std::string &name)
        : _path(path), _name(name), _fd(-1)
    {
        _fifoname = _path + "/" + _name;
    }
    void OpenForRead()
    {
        // 打开文件
        _fd = open(_fifoname.c_str(), O_RDONLY);
        if (_fd < 0)
        {
            std::cout << "open fifo error" << std::endl;
            return;
        }
        std::cout << "open fifo successful" << std::endl;
    }
    void OperForWrite()
    {
        // 打开文件
        _fd = open(_fifoname.c_str(), O_WRONLY);
        if (_fd < 0)
        {
            std::cout << "open fifo error" << std::endl;
            return;
        }
        std::cout << "open fifo successful" << std::endl;
    }
    void Read()
    {
          // 读
        while (true)
        {
            char buffer[1024];
            int number = read(_fd, buffer, sizeof(buffer) - 1);
            if (number > 0)
            {
                buffer[number] = 0;
                std::cout << "client say:" << buffer << std::endl;
            }
            else if (number == 0)
            {
                std::cout << "ending" << std::endl;
                break;
            }
            else
            {
                std::cout << "read failed" << std::endl;
                break;
            }
        }
    }
    void Write()
    {
        // 进行写入
        std::string message;
        int cnt = 1;
        pid_t id = getpid();
        std::cout << "Place Enter---" << std::endl;
        while (true)
        {
            std::getline(std::cin, message);
            message += (",message number") + std::to_string(cnt++) + ",[" + std::to_string(id) + "]";
            write(_fd, message.c_str(), message.size());
        }
    }
    void Close()
    {
        if (_fd > 0)
            close(_fd);
    }
    ~FifoOper()
    {
    }

private:
    std::string _name;
    std::string _path;
    std::string _fifoname;
    int _fd;
};
